define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'merchant/index' + location.search,
                    add_url: 'merchant/add',
                    edit_url: 'merchant/edit',
                    //del_url: 'merchant/del',
                    multi_url: 'merchant/multi',
                    import_url: 'merchant/import',
                    table: 'merchant',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                visible: false,
                showToggle: false,
                showColumns: false,
                search: false,
                showExport: false,
                escape: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'logo', title: __('Logo'), operate: false,operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                        /*{field: 'lon', title: __('Lon'), operate: 'LIKE'},
                        {field: 'lat', title: __('Lat'), operate: 'LIKE'},*/
                        {
                            field: 'door_image',
                            title: __('Door_image'),
                            operate: false,
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image
                        },
                        {
                            field: 'license_image',
                            title: __('License_image'),
                            operate: false,
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image
                        },
                        {field: 'door_contacts', title: __('Door_contacts'), operate: 'LIKE'},
                        {field: 'telephone', title: __('Telephone'), operate: 'LIKE'},
                        {field: 'equity', title: __('Equity'), operate:false, formatter: Controller.api.formatter.goods},
                        {field: 'money', title: __('Money'), operate: false},
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: {"0": __('Status 0'), "1": __('Status 1'), "2": __('Status 2'), "3": __('Status 3')},
                            formatter: Table.api.formatter.status
                        },
                        {field: 'remark', title: __('Remark'), operate: false},
                        {
                            field: 'openswitch',
                            title: __('Openswitch'),
                            searchList: {"0": __('Openswitch 0'), "1": __('Openswitch 1')},
                            table: table,
                            formatter: Table.api.formatter.toggle
                        },
                        {
                            field: 'Opentime',
                            title: __('Opentime'),
                            table: table,
                            formatter: function (row,index) {
                                return index.start_time+" - "+index.end_time;
                            }
                        },

                        /* {field: 'account_number', title: __('Account_number'), operate: 'LIKE'},
                         {field: 'account_image', title: __('Account_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                         */
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate', title: __('Operate'), table: table,
                            buttons: [
                                {
                                    name: 'see',
                                    title: __('查看'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    text: '查看',
                                    //icon: 'fa fa-folder-o',
                                    url: function (row, column) {
                                        var url = 'merchant/see?id=' + row.id;
                                        return url;
                                    },
                                },
                                {
                                    name: 'ok',
                                    title: '通过',
                                    text: '通过',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: '',
                                    confirm: '确定通过审核？',
                                    visible: function (row) {
                                        // console.log(row)
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.status == 1) {
                                            return false
                                        }
                                        return true;

                                    },
                                    url: function (row, column) {
                                        var url = 'merchant/operation?id=' + row.id + '&status=1';
                                        return url;
                                    },
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                },
                                {
                                    name: 'operation',
                                    classname: 'btn btn-xs btn-info btn-refund',
                                    text: "驳回",
                                    icon: "",
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.status == 1 || row.status==2) {
                                            return false
                                        }
                                        return true;

                                    }
                                },
                                {
                                    name: 'disable',
                                    title: '禁用',
                                    text: '禁用',
                                    classname: 'btn btn-xs btn-warning btn-magic btn-ajax',
                                    icon: '',
                                    confirm: '确定禁用店铺？',
                                    visible: function (row) {
                                        // console.log(row)
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.status != 1) {
                                            return false
                                        }
                                        return true;

                                    },
                                    url: function (row, column) {
                                        var url = 'merchant/operation?id=' + row.id + '&status=3';
                                        return url;
                                    },
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                },
                                {
                                    name: 'merchantuser',
                                    title: __('店铺员工'),
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    text: '店铺员工',
                                    //icon: 'fa fa-folder-o',
                                    url: function (row, column) {
                                        var url = 'merchantuser/index?merchant_id=' + row.id;
                                        return url;
                                    },
                                },
                                {
                                    name: 'merchantgoods',
                                    title: __('自营商品'),
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    text: '自营商品',
                                    //icon: 'fa fa-folder-o',
                                    url: function (row, column) {
                                        var url = 'merchantgoods/index?card_type_id=0&merchant_id=' + row.id;
                                        return url;
                                    },
                                },
                                {
                                    name: 'member',
                                    title: __('推广用户'),
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    text: '推广用户',
                                    //icon: 'fa fa-folder-o',
                                    url: function (row, column) {
                                        var url = 'member/index?merchant_id=' + row.id;
                                        return url;
                                    },
                                },
                                {
                                    name: 'washlog',
                                    title: __('洗车记录'),
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    text: '洗车记录',
                                    //icon: 'fa fa-folder-o',
                                    url: function (row, column) {
                                        var url = 'washlog/index?merchant_id=' + row.id;
                                        return url;
                                    },
                                },
                            ]
                            , events: {
                                'click .btn-refund': function (e, value, row) {
                                    Layer.prompt({
                                        title: "请输入备注",
                                        formType: 2,
                                        area: ['200px', '100px'], //自定义文本域宽高
                                        value: row.remark,
                                    }, function (value) {
                                        Fast.api.ajax({
                                            url: "merchant/operation?id=" + row.id + '&status=2',
                                            data: {remark: value},
                                        }, function (data, ret) {
                                            Layer.closeAll();
                                            $(".btn-refresh").trigger("click");
                                        });
                                    });
                                    return false;
                                },
                                'click .btn-delone': function (e, value, row, index) {
                                    e.stopPropagation();
                                    e.preventDefault();
                                    var that = this;
                                    var top = $(that).offset().top - $(window).scrollTop();
                                    var left = $(that).offset().left - $(window).scrollLeft() - 260;
                                    if (top + 154 > $(window).height()) {
                                        top = top - 154;
                                    }
                                    if ($(window).width() < 480) {
                                        top = left = undefined;
                                    }
                                    Layer.confirm(
                                        __('Are you sure you want to delete this item?'),
                                        {
                                            icon: 3,
                                            title: __('Warning'),
                                            offset: [top, left],
                                            shadeClose: true,
                                            btn: [__('OK'), __('Cancel')]
                                        },
                                        function (index) {
                                            var table = $(that).closest('table');
                                            var options = table.bootstrapTable('getOptions');
                                            Table.api.multi("del", row[options.pk], table, that);
                                            Layer.close(index);
                                        }
                                    );
                                },
                                'click .btn-editone': function (e, value, row, index) {
                                    e.stopPropagation();
                                    e.preventDefault();
                                    var table = $(this).closest('table');
                                    var options = table.bootstrapTable('getOptions');
                                    var ids = row[options.pk];
                                    row = $.extend({}, row ? row : {}, {ids: ids});
                                    var url = options.extend.edit_url;
                                    Fast.api.open(Table.api.replaceurl(url, row, table), $(this).data("original-title") || $(this).attr("title") || __('Edit'), $(this).data() || {});
                                },
                            }, formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $(document).on('click', '.btn-close', function () {
                Fast.api.close($("#c-address").val()+","+$("#c-address").attr("data-three")+","+$("#c-address").attr("data-one"));
            });
            Controller.api.bindevent();
        },
        edit: function () {
            $(document).on('click', '.btn-close', function () {
                Fast.api.close($("#c-address").val()+","+$("#c-address").attr("data-three")+","+$("#c-address").attr("data-one"));
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                goods: function (value, row, index) {
                    //这里手动构造URL
                    url = "merchantgoods?type_data=3&merchant_id=" +  row.id;
                    this.url = url;
                    return '<a href="' + url + '" class="label label-success addtabsit" title="' + __("Search %s", row.name) + '权益卡券">' +  value + '</a>';
                    return Table.api.formatter.addtabs.call(this, value, row, index);
                },
            },
        }
    };
    return Controller;
});
$("[data-toggle='addresspicker']").data("callback", function(res){
    //其中res则是包含了address/lat/lng等信息的JSON对象
    if(res!=''){
        $(".address").removeClass("has-error");
        $(".address").addClass("has-success");
        $("span[for=\"c-address\"]").attr("style","display:none");
    }
    $("#classify").val(res.address3)
});
